﻿using System;
using System.Windows;
using System.Threading;
using System.Collections;
using System.Xml;
using System.Windows.Threading;

namespace DownloadTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //计时器
        public DispatcherTimer dispatcherTimer;
        Thread t = null;
        //储存任务列表的URL
        String[] ls = null;
        //标记是否已经开始下载
        bool isStart = false;
        public MainWindow()
        {
            InitializeComponent();


            //  DispatcherTimer setup
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        }
        private void Open_Click(object sender, RoutedEventArgs e)
        {
            Util.Folder.open(Achi.Download.dir);
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {

            if (!isStart)
            {
                IList l = MusicList.SelectedItems;
                if (l.Count == 0)
                {
                    MessageBox.Show("请选择要下载的文件");
                    return;
                }
                ls = new String[l.Count];
                int i = 0;
                foreach (XmlElement c in l)
                {
                    ls[i++] = c.InnerText;
                }

                t = new Thread(new ThreadStart((new Achi.Download(ls)).download));
                t.SetApartmentState(ApartmentState.STA);
                try
                {
                    t.Start();
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message);
                }
                dispatcherTimer.Start();
                StartDown.Content = "停止下载";
                isStart = true;
            }
            else
            {

                dispatcherTimer.Stop();
                Achi.Download.stop();

                StartDown.Content = "开始下载";
                isStart = false;
                NowTask.Text = "已终止下载";

            }

        }


        int sty = 0;
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (t.IsAlive)
                {
                    if (sty < 10)
                    {
                        sty++;
                    }
                    else
                    {
                        sty = 0;
                    }
                    NowTask.Text = Achi.Download.Task.Substring(sty);
                }

                else
                {
                    dispatcherTimer.Stop();
                    StartDown.Content = "开始下载";
                    isStart = false;
                    NowTask.Text = "已完成下载任务";

                }
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message, "ErrorTimer");
            }
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            Achi.Download.stop();
            Achi.Download.closeStream();

            Application.Current.Shutdown();
        }

    }

}
